#include "../../mqserver/consumer.hpp"
#include "../../mqcommon/msg.pb.h"
#include <gtest/gtest.h>

QueueConsumerManager::Ptr managerPtr;
class ConsumerTest : public testing::Environment
{
public:
    virtual void SetUp() override
    {
        managerPtr = std::make_shared<QueueConsumerManager>();
        managerPtr->initQueueConsumer("queue1");
    }
    virtual void TearDown() override
    {
        managerPtr->clear();
    }
};

void cb(const std::string& tag, const BasicProperty* bp, const std::string& body)
{
    DLOG("%s,消费%s", tag.c_str(), body.c_str());
}
TEST(ConsumerTest, create)      //recovery也没问题
{
    managerPtr->create("consumer1", "queue1", false, cb);
    managerPtr->create("consumer2", "queue1", false, cb);
    managerPtr->create("consumer3", "queue1", false, cb);

    EXPECT_EQ(managerPtr->exists("queue1", "consumer1"), true);
    EXPECT_EQ(managerPtr->exists("queue1", "consumer2"), true);
    EXPECT_EQ(managerPtr->exists("queue1", "consumer3"), true);
}

TEST(ConsumerTest, remove)      //recovery也没问题
{
    managerPtr->remove("queue1", "consumer1");
    EXPECT_EQ(managerPtr->exists("queue1", "consumer1"), false);
    EXPECT_EQ(managerPtr->exists("queue1", "consumer2"), true);
    EXPECT_EQ(managerPtr->exists("queue1", "consumer3"), true);
}

TEST(ConsumerTest, choose)      //recovery也没问题
{
    Consumer::Ptr ConsumerPtr = managerPtr->choose("queue1");
    EXPECT_EQ(ConsumerPtr->tag, "consumer2");

    ConsumerPtr = managerPtr->choose("queue1");
    EXPECT_EQ(ConsumerPtr->tag, "consumer3");

    ConsumerPtr = managerPtr->choose("queue1");
    EXPECT_EQ(ConsumerPtr->tag, "consumer2");

    ConsumerPtr = managerPtr->choose("queue1");
    EXPECT_EQ(ConsumerPtr->tag, "consumer3");
}

int main(int argc, char* argv[])
{
    testing::InitGoogleTest(&argc, argv);
    testing::AddGlobalTestEnvironment(new ConsumerTest);

    return RUN_ALL_TESTS(); 
}